import React, {Component} from 'react';
import {View, Text, Button, DevSettings} from 'react-native';
import {getLogger} from '@siteed/react-native-logger';
import RNRestart from 'react-native-restart'; // Import package from node modules

const logger = getLogger('App');
class ErrorBoundary extends Component<
  {children: React.ReactNode},
  {hasError: boolean}
> {
  constructor(props: {children: React.ReactNode}) {
    super(props);
    this.state = {hasError: false};
  }

  static getDerivedStateFromError(error: unknown) {
    return {hasError: true};
  }

  componentDidCatch(error: any, errorInfo: any) {
    // 在这里记录错误日志
    console.log('记录错误日志:', error, errorInfo);
    logger.error('发生错误:', error, errorInfo);
    setTimeout(() => {
      RNRestart.restart();
    }, 10 * 1000);
  }

  render() {
    if (this.state.hasError) {
      return (
        <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
          <Text>发生错误，10秒后重新加载...</Text>
        </View>
      );
    }

    return this.props.children;
  }
}

export default ErrorBoundary;
